}
static gint
-pnm_read_ascii_scanline (PnmLoaderContext *context)
+pnm_read_ascii_mono_scanline (PnmLoaderContext *context)
{
PnmIOBuffer *inbuf;
- guint offset;
- guint value, numval, i;
- guchar data;
- guchar mask;
- guchar *dptr;
+ guint value;
gint retval;
+ guchar *dptr;
gint max_length;
-
- g_return_val_if_fail (context != NULL, PNM_FATAL_ERR);
-
- data = mask = 0;
-
- inbuf = &context->inbuf;
-
- context->dptr = context->pixels + context->output_row * context->rowstride;
-
- switch (context->type) {
- case PNM_FORMAT_PBM:
+
+ if (context->type == PNM_TYPE_PBM)
max_length = 1;
- numval = MIN (8, context->width - context->output_col);
- offset = context->output_col / 8;
- break;
- case PNM_FORMAT_PGM:
- max_length = -1;
- numval = 1;
- offset = context->output_col;
- break;
- case PNM_FORMAT_PPM:
+ else
max_length = -1;
- numval = 3;
- offset = context->output_col * 3;
- break;
-
- default:
- g_set_error (context->error,
- GDK_PIXBUF_ERROR,
- GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
- _("PNM image format is invalid"));
- return PNM_FATAL_ERR;
+ inbuf = &context->inbuf;
+
+ context->dptr = context->pixels + context->output_row * context->rowstride;
+
+ dptr = context->dptr + context->output_col * 3;
+
+ while (TRUE) {
+ retval = pnm_read_next_value (inbuf, max_length, &value, context->error);
+ if (retval != PNM_OK)
+ return retval;
+
+ if (context->type == PNM_TYPE_PBM) {
+ value = value ? 0 : 0xff;
+ }
+ else {
+ /* scale the color up or down to an 8-bit color depth */
+ if (value > context->maxval)
+ value = 255;
+ else
+ value = (guchar)(255 * value / context->maxval);
+ }
+
+ *dptr++ = value;
+ *dptr++ = value;
+ *dptr++ = value;
+
+ context->output_col++;
+
+ if (context->output_col == context->width) {
+ context->output_col = 0;
+ context->output_row++;
+ break;
+ }
}
+
+ return PNM_OK;
+}
+
+static gint
+pnm_read_ascii_color_scanline (PnmLoaderContext *context)
+{
+ PnmIOBuffer *inbuf;
+ guint value, i;
+ guchar *dptr;
+ gint retval;
+
+ inbuf = &context->inbuf;
+
+ context->dptr = context->pixels + context->output_row * context->rowstride;
- dptr = context->dptr + offset + context->scan_state;
+ dptr = context->dptr + context->output_col * 3 + context->scan_state;
while (TRUE) {
- if (context->type == PNM_FORMAT_PBM) {
- mask = 0x80;
- data = 0;
- numval = MIN (8, context->width - context->output_col);
- }
-
- for (i = context->scan_state; i < numval; i++) {
- retval = pnm_read_next_value (inbuf, max_length,
- &value, context->error);
+ for (i = context->scan_state; i < 3; i++) {
+ retval = pnm_read_next_value (inbuf, -1, &value, context->error);
if (retval != PNM_OK) {
/* save state and return */
context->scan_state = i;
return retval;
}
- switch (context->type) {
- case PNM_FORMAT_PBM:
- if (value)
- data |= mask;
- mask >>= 1;
-
- break;
- case PNM_FORMAT_PGM:
- case PNM_FORMAT_PPM:
- /* scale the color up or down to an 8-bit color depth */
- if (value > context->maxval)
- *dptr++ = 255;
- else
- *dptr++ = (guchar)(255 * value / context->maxval);
- break;
- default:
- g_set_error (context->error,
- GDK_PIXBUF_ERROR,
- GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
- _("PNM image format is invalid"));
- return PNM_FATAL_ERR;
- break;
- }
+ if (value > context->maxval)
+ *dptr++ = 255;
+ else
+ *dptr++ = (guchar)(255 * value / context->maxval);
}
context->scan_state = 0;
-
- if (context->type == PNM_FORMAT_PBM) {
- *dptr++ = data;
- context->output_col += numval;
- } else {
- context->output_col++;
- }
+ context->output_col++;
if (context->output_col == context->width) {
- if (context->type == PNM_FORMAT_PBM)
- explode_bitmap_into_buf (context);
- else if (context->type == PNM_FORMAT_PGM)
- explode_gray_into_buf (context);
-
context->output_col = 0;
context->output_row++;
break;
break;
case PNM_FORMAT_PBM:
case PNM_FORMAT_PGM:
+ retval = pnm_read_ascii_mono_scanline (context);
+ if (retval != PNM_OK)
+ return retval;
+ break;
case PNM_FORMAT_PPM:
- retval = pnm_read_ascii_scanline (context);
+ retval = pnm_read_ascii_color_scanline (context);
if (retval != PNM_OK)
return retval;
break;